package com.sz.sboot.controller;


import com.sz.sboot.common.PageBean;
import com.sz.sboot.common.ResoultMsg;
import com.sz.sboot.entity.TUser;
import com.sz.sboot.service.ITUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;

@CrossOrigin //代表可以跨域访问
@Api(value = "/tuser",description = "用户管理")
@RestController
@RequestMapping("/tuser")  //配置一个请求访问地址,用在类上,表示该类中所有字地址都是以它为父路径
public class TUserController {

    @Autowired
    private ITUserService itUserService;

    //模糊查询所有用户
    @ApiOperation(value = "模糊查询所有用户(带分页)",notes = "模糊查询所有用户(带分页)")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "pageIndex",value = "当前页码数",required = true,dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页多少条",required = true,dataType = "int"),
            @ApiImplicitParam(name = "tUser",value = "查询条件",required = false,dataType = "TUser")})
    @PostMapping("/list")
    public PageBean<TUser> list(int pageIndex,
                                int pageSize,
                                @RequestBody(required = false) TUser tUser){
        return itUserService.getAllUsers(pageIndex,pageSize,tUser);
    }

    //删除一个用户
    @ApiOperation(value = "根据uid删除一个用户",notes = "根据uid删除一个用户")
    @ApiImplicitParam(name = "uid",value = "用户的uid",required = true,dataType = "int",paramType = "path")
    @GetMapping("/deleteUsers/{uid}")
    public ResoultMsg deleteUsers(@PathVariable("uid") int uid){
        int n=itUserService.deleteUsers(uid);
        if (n>0){
            return new ResoultMsg(1,"删除成功");
        }else {
            return new ResoultMsg(101,"删除失败");
        }
    }

    //查询一个用户
    @ApiOperation(value = "根据uid查询一个用户",notes = "根据uid查询一个用户")
    @ApiImplicitParam(name = "uid",value = "用户的uid",required = true,dataType = "int",paramType = "path")
    @GetMapping("/getUsersById/{uid}")
    public TUser getUsersById(@PathVariable("uid") int uid){
        return itUserService.getUsersById(uid);
    }

    //添加或修改一个用户
    @ApiOperation(value = "根据页面传输的值添加或修改",notes = "根据页面传输的值添加或修改")
    @ApiImplicitParam(name = "tUser",value = "用户输入的值",required = true,dataType = "TUser")
    @PostMapping("/addorupadte")
    public ResoultMsg addorupadte(@RequestBody TUser tUser){
        ResoultMsg resoultMsg=null;
        if (tUser.getUid()==null){
            //表示添加
            if (itUserService.addUsers(tUser)>0){
                resoultMsg=new ResoultMsg(1,"操作成功");
            }else {
                resoultMsg=new ResoultMsg(101,"操作失败");
            }
        }else {
            //表示修改
            if (itUserService.updateUsers(tUser)>0){
                resoultMsg=new ResoultMsg(1,"操作成功");
            }else {
                resoultMsg=new ResoultMsg(101,"操作失败");
            }
        }
        return resoultMsg;
    }

    //批量删除
    @ApiOperation(value = "根据多个uid批量删除用户",notes = "根据多个uid批量删除用户")
    @ApiImplicitParam(name = "uids",value = "用户id的集合",required = true,dataType = "int",allowMultiple = true)
    @PostMapping("/piliang")
    public ResoultMsg piliang(@RequestBody ArrayList<Integer> uids){
        ResoultMsg resoultMsg=null;
        if (itUserService.deleteByUids(uids)>0){
            resoultMsg=new ResoultMsg(1,"删除成功");
        }else {
            resoultMsg=new ResoultMsg(101,"删除失败");
        }
        return resoultMsg;
    }
}